<?php
/*
 * 手机客户端 最高出价信息表函数
 * 2013-11-26
 */
 
 
 /**
  * 获取竞拍中领先展品总数
  */
 function userlastbid_getDealAuctionCount($uid){
	$sql = "
			select
				count(1) as rc
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
			where
				a.uid = '".$uid."' 
				and a.is_bid = 1
				and a.pay_status=0
			and a.aid in (select aid from auction where asid in (select asid from auction_special where now() between asopentime and asclosetime))
	";
	$db = $GLOBALS['db'];
	$db->query($sql);
	
	if($db->next_record()){
		return $db->f("rc");
	}
	return 0;
 }
 
 
 /**
  * 用户已得标的展品数
  */
 function userlastbid_userBuyerWonCount($uid){
	$sql = "
			select
					count(1) as rc
			from
					auction_userlastbid a
					inner join auction b on b.aid = a.aid
			where
					a.uid = '".$uid."'
					and a.is_bid=1 
					and a.aid in (select aid from auction where asid in (select asid from auction_special where now() > asclosetime))
	";
	$db = $GLOBALS['db'];
	$db->query($sql);
	
	if($db->next_record()){
		return $db->f("rc");
	}
	return 0;
 }
 
 
 /**
  * 用户未得标的展品数 （包含专场进行中和专场已结束的数据）
  */
 function userlastbid_userBuyerDontWinCount($uid){
	$sql = "
			select
				count(1) as rc
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
			where
				a.uid = '".$uid."'
				and a.is_bid = 0 
				and b.asid in (select asid from auction_special where asclosetime>now())
	";
	$db = $GLOBALS['db'];
	$db->query($sql);
	
	if($db->next_record()){
		return $db->f("rc");
	}
	return 0;
 }
 
 
 /**
  * 用户未支付的拍品数量
  */
 function userlastbid_userUnIntoPaidCount($uid){
	$sql = "
			select
				count(1) as rc
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
			where
				a.uid = '".$uid."'
				and a.is_bid = 1
				and a.is_bid = 1
				and a.pay_status=0
				and a.aid in (select aid from auction where asid in (select asid from auction_special where now() > asclosetime)) 
	";
	$db = $GLOBALS['db'];
	$db->query($sql);
	
	if($db->next_record()){
		return $db->f("rc");
	}
	return 0;
 }
 
 /**
  * 会员竞拍中
  */
 function userlastbid_userbuyerbid($uid){
	$sql = "
		select distinct 
			b.aid,
			b.acode,
			b.auction,
			b.lastprice,
			date_format(c.asprevtime,'%Y-%m-%d %H:%i:%s') as prevtime,
			date_format(c.asopentime,'%Y-%m-%d %H:%i:%s') as starttime,
			date_format(c.asclosetime,'%Y-%m-%d %H:%i:%s') as endtime,
			if(c.asprevtime>now(),'-1',if(c.asprevtime<now() and c.asprevclosetime>now(),0,if(c.asopentime<now() and c.asclosetime>now(),1,2))) as auctiontype,
			c.astag as atag 
		from
			auction_userlastbid a
			inner join auction b on b.aid = a.aid
			inner join auction_special c on b.asid = c.asid
		where
			a.uid = '".$uid."' 
			and c.astag>0
			and a.aid in (select aid from auction where asid in (select asid from auction_special where now() between asopentime and asclosetime))
		order by
			c.asclosetime desc
	";
	$db = $GLOBALS['db'];
	return $db->queryArray($sql);
 }
 
 
 /**
  * 已得标
  */
 function userlastbid_userwon($uid){
	$sql = "
			select
				b.aid,
				b.acode,
				b.auction,
				b.lastprice,
				a.bid,
				date_format(c.asprevtime,'%Y-%m-%d %H:%i:%s') as prevtime,
				date_format(c.asopentime,'%Y-%m-%d %H:%i:%s') as starttime,
				date_format(c.asclosetime,'%Y-%m-%d %H:%i:%s') as endtime,
				if(c.asprevtime>now(),'-1',if(c.asprevtime<now() and c.asprevclosetime>now(),0,if(c.asopentime<now() and c.asclosetime>now(),1,2))) as auctiontype,
				a.is_bid,
				a.wonid,
				c.astag as atag
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
				inner join auction_special c on b.asid=c.asid
			where
				a.uid = '".$uid."'
				and a.is_bid = 1
				and c.astag > 0
				and c.asclosetime < now()
	";
	$db = $GLOBALS['db'];
	return $db->queryArray($sql);
 }
 
/**
  * 未得标
  * $uid 用户主键
  * $attention 关注类型 0排除 1包含
  */
 function userlastbid_userdontwin($uid,$attention,$gzIds){
	$sql = "
			select
				distinct 
				b.aid,
				b.acode,
				b.auction,
				b.lastprice,
				date_format(c.asprevtime,'%Y-%m-%d %H:%i:%s') as prevtime,
				date_format(c.asopentime,'%Y-%m-%d %H:%i:%s') as starttime,
				date_format(c.asclosetime,'%Y-%m-%d %H:%i:%s') as endtime,
				if(c.asprevtime>now(),'-1',if(c.asprevtime<now() and c.asprevclosetime>now(),0,if(c.asopentime<now() and c.asclosetime>now(),1,2))) as auctiontype,
				a.is_bid,
				c.astag as atag
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
				inner join auction_special c on b.asid = c.asid
			where
				a.uid = '".$uid."'
				and a.is_bid = 1
				and c.astag > 0
				and a.pay_status=0
				and a.aid in (select aid from auction where asid in (select asid from auction_special where now() > asclosetime)) 
	";
	if(!$gzIds){
		$gzIds = "0";
	}
	if($attention=="0"){
		$sql = $sql." and b.aid not in (".$gzIds.")";
	}else if($attention=="1"){
		$sql = $sql." and b.aid in (".$gzIds.")";
	}
	$sql = $sql." order by b.endtime desc";
	$db = $GLOBALS['db'];
	return $db->queryArray($sql);
 }
 
 /**
  * 未支付
  */
 function userlastbid_userwonunpay($uid){
	$sql = "
			select
				b.aid,
				b.acode,
				b.auction,
				b.pcid,
				a.bid,
				date_format(c.asprevtime,'%Y-%m-%d %H:%i:%s') as prevtime,
				date_format(c.asopentime,'%Y-%m-%d %H:%i:%s') as starttime,
				date_format(c.asclosetime,'%Y-%m-%d %H:%i:%s') as endtime,
				if(c.asprevtime>now(),'-1',if(c.asprevtime<now() and c.asprevclosetime>now(),0,if(c.asopentime<now() and c.asclosetime>now(),1,2))) as auctiontype,
				a.is_bid,
				b.lastprice,
				b.servicefee,
				c.astag as atag
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
				inner join auction_special c on b.asid=c.asid
			where
				a.uid = '".$uid."' 
				and a.is_bid = 1
				and a.pay_status=0
				and a.aid in (select aid from auction where asid in (select asid from auction_special where now() > asclosetime)) 
			order by
				c.asclosetime asc
	";
	$db = $GLOBALS['db'];
	return $db->queryArray($sql);
 }
 
 
/**
  * 我的领先
  */
 function userlastbid_lingxian($uid){
	$sql = "
			select
				b.aid,
				b.acode,
				b.auction,
				b.pcid,
				a.bid,
				date_format(c.asprevtime,'%Y-%m-%d %H:%i:%s') as prevtime,
				date_format(c.asopentime,'%Y-%m-%d %H:%i:%s') as starttime,
				date_format(c.asclosetime,'%Y-%m-%d %H:%i:%s') as endtime,
				if(c.asprevtime>now(),'-1',if(c.asprevtime<now() and c.asprevclosetime>now(),0,if(c.asopentime<now() and c.asclosetime>now(),1,2))) as auctiontype,
				a.is_bid,
				b.lastprice,
				b.servicefee,
				c.astag as atag
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
				inner join auction_special c on c.asid = b.asid
			where
				a.uid = '".$uid."' 
				and c.astag>0
				and a.is_bid = 1
				and a.pay_status=0
				and a.aid in (select aid from auction where asid in (select asid from auction_special where now() < asclosetime and now()>asopentime)) 
			order by
				c.asclosetime asc
	";
	$db = $GLOBALS['db'];
	return $db->queryArray($sql);
 }
 
 
 /**
  * 我的出局
  */
 function userlastbid_chuju($uid){
	$sql = "
			select
				b.aid,
				b.acode,
				b.auction,
				b.pcid,
				a.bid,
				date_format(c.asprevtime,'%Y-%m-%d %H:%i:%s') as prevtime,
				date_format(c.asopentime,'%Y-%m-%d %H:%i:%s') as starttime,
				date_format(c.asclosetime,'%Y-%m-%d %H:%i:%s') as endtime,
				if(c.astag=0,'待审',if(c.asprevtime>now(),'-1',if(c.asprevtime<now() and c.asprevclosetime>now(),0,if(c.asopentime<now() and c.asclosetime>now(),1,2)))) as
				auctiontype,
				a.is_bid,
				b.lastprice,
				b.servicefee
			from
				auction_userlastbid a
				inner join auction b on b.aid = a.aid
				inner join auction_special c on b.asid=c.asid
			where
				a.uid = '".$uid."' 
				and a.is_bid = 0 
				and b.asid in (select asid from auction_special where asclosetime>now())
			order by
				c.asclosetime asc
	";
	$db = $GLOBALS['db'];
	return $db->queryArray($sql);
 }
 ?>